﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Bling.DSL;
using Bling.WPF;
using Bling.Core;

namespace Bling.Tutorial {
  using Bling.Graphics;
  using Bling.Shapes;
  public class CurveTest : Canvas {
    public CurveTest() {
      CanvasBl Canvas = this;
      var Ts = new PointBl[4];

      for (int i = 0; i < 4; i++) {
        double ratio = i;
        ratio = ratio / (Ts.Length - 1);
        Ts[i] = new ThumbBl(Canvas) {
          CanDrag = true,
          Background = i.SelectColor(),
          ZIndex = 1,
          Left = 10d + (Canvas.Width - 20d) * ratio,
          Top = 10d.Bl() + (i % 2 == 0 ? 0d.Bl() : Canvas.Height / 2d),
        }.CenterPosition;
      }
      var segment = new BezierSegmentBl() {
        Point1 = Ts[1],
        Point2 = Ts[2],
        Point3 = Ts[3],
      };
      var curve = new PCurve(t => t.Bezier(Ts));
      var shape = new PathShapeBl(Canvas) {
        ZIndex = 0,
        Start = Ts[0],
        Segments = { AddOne = segment },
      };
      var Slider = new LabelSliderBl(Canvas) {
        LabelName = "T",
        LeftBottom = new PointBl(0, Canvas.Height),
        Minimum = 0,
        Maximum = 1,
        ZIndex = 1,
      };

      var At = new ThumbBl(Canvas) {
        Background = Ts.Length.SelectColor(),
        CenterPosition = Slider.Value.Bezier(Ts),
        ZIndex = 1,
      };

      //Console.WriteLine("CURVE0: " + curve);
      //Console.WriteLine("CURVEd: " + curve.Normals(Slider.Value));

      new LineBl(Canvas) {
        Start = At.CenterPosition,
        End = At.CenterPosition + curve.Normals(Slider.Value) * -50d,

      };
      {
        var eval = new Linq.DefaultLinqEval();
        Console.WriteLine("CURVE");
        Console.WriteLine(eval.Eval000<double, Point>(d => curve[d]).Dump); ;
      }
      {
        var eval = new Linq.DefaultLinqEval();
        Console.WriteLine("DERIVATIVE");
        Console.WriteLine(eval.Eval000<double, Point>(d => curve.Normals(d)).Dump); ;
      }
    }
  }
}
